package com.engineering.elink.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.engineering.elink.co.BaseCo;
import com.engineering.elink.co.CourseAddCo;
import com.engineering.elink.co.CourseCo;
import com.engineering.elink.common.ErrorType;
import com.engineering.elink.service.CourseService;
import com.engineering.elink.util.ResJsonUtil;

/**
 * 课程restful接口及请求转发，支持跨域
 */
@RestController
@CrossOrigin
@RequestMapping("/api/course")
@Api(value="课程接口")
public class CourseController {

	@Autowired
	private CourseService courseService;
	
	
	/**
	 * 课程列表
	 */
	@RequestMapping(value="/getList",method = RequestMethod.GET)
	@ApiOperation(value = "课程列表")
	public Map<String, Object> getList(@ModelAttribute @Valid CourseCo co,HttpServletRequest request){
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS, courseService.getCoursesList(co,""));
	}
	
	/**
	 * 发布课程
	 */
	@RequestMapping(value="/add",method = RequestMethod.POST)
	@ApiOperation(value = "发布课程")
	public Map<String, Object> add(@ModelAttribute @Valid CourseAddCo addCo,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
			return courseService.add(addCo,userId);
		}
		
		return ResJsonUtil.getMap(ErrorType.NEED_LOGIN);
	}
	
	/**
	 * 修改课程
	 */
	@RequestMapping(value="/update",method = RequestMethod.POST)
	@ApiOperation(value="修改课程")
	public Map<String, Object> update(@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,
			@ModelAttribute @Valid CourseAddCo addCo,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
			return courseService.update(courseId,addCo,userId);
		}
		
		return ResJsonUtil.getMap(ErrorType.NEED_LOGIN);
	}
	
	@RequestMapping(value="/updateType",method = RequestMethod.POST)
	@ApiOperation(value="修改课程文件分类")
	public Map<String, Object> updateType(@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,
			@ApiParam(value="课程分类ID",required=true)@RequestParam(value="typeId",required=true)Integer typeId){
		
		
		return courseService.updateType(courseId,typeId);
	}
	
	/**
	 * 获取热门推荐课程
	 */
	@RequestMapping(value="/getHotCourse",method=RequestMethod.GET)
	@ApiOperation(value="热门推荐课程")
	public Map<String,Object> getHotCourse(@ModelAttribute @Valid BaseCo co){
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,courseService.getHotCourse(co));
	}
	
	/**
	 * 
	 * 获取最近浏览课程
	 */
	@RequestMapping(value="/getBrowseCourse",method=RequestMethod.GET)
	@ApiOperation(value="最近浏览课程")
	public Map<String,Object> getBrowseCourse(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		if(StringUtils.isNotEmpty(userId)){
			return ResJsonUtil.getMap(ErrorType.SUCCESS,courseService.getBrowseCourse(co));
		}
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS);
	}
	
	/**
	 * 
	 * 单个课程详情信息
	 */
	@RequestMapping(value="/get",method = RequestMethod.GET)
	@ApiOperation(value = "课程详情")
	public Map<String, Object> get(
			@ApiParam(value="课程id",required=true) @RequestParam(value = "courseId",required=true)Integer courseId,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		
		String userId=null;
		if(userInfo!=null && userInfo.get("userId")!=null){
			userId =  userInfo.get("userId").toString();
		}
		
		return courseService.get(courseId,userId);
	}
	
	/**
	 * 
	 * 获取导师下面的最近6条课程
	 */
	@RequestMapping(value="/getTeacherCourse",method=RequestMethod.GET)
	@ApiOperation(value="讲师课程")
	public Map<String,Object> getTeacherCourse(
			@ApiParam(value="讲师ID",required=true) @RequestParam(value = "userId",required=true)String userId){
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,courseService.getTeacherCourse(null, userId));
	}
	
	/**
	 * 获取6条相同分类的课程
	 */
	@RequestMapping(value="/getSameCourse",method=RequestMethod.GET)
	@ApiOperation(value="相关课程")
	public Map<String,Object> getSameCourse(@ModelAttribute @Valid BaseCo co,
			@ApiParam(value="当前课程id",required=false) @RequestParam(value = "courseId",required=false)Integer courseId){
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,courseService.getSameCourse(co,courseId));
	}
	
	/**
	 * 
	 * 获取我的课程
	 */
	@RequestMapping(value="/getMyCourse",method=RequestMethod.GET)
	@ApiOperation(value="我的视频")
	public Map<String,Object> getMyCourse(@ModelAttribute @Valid BaseCo co,HttpServletRequest request){
		
		@SuppressWarnings("unchecked")
		Map<String,Object> userInfo  = (Map<String, Object>) request.getAttribute("userInfo");
		String userId =  userInfo.get("userId").toString();
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,courseService.getMyCourse(co,userId));
	}
	
	@RequestMapping(value="/getHeCourse",method=RequestMethod.GET)
	@ApiOperation(value="他的视频")
	public Map<String,Object> getHeCourse(@ApiParam(value="用户ID",required=true)@RequestParam(value="userId",required=true)String userId,@ModelAttribute @Valid BaseCo co){
		
		return ResJsonUtil.getMap(ErrorType.SUCCESS,courseService.getMyCourse(co,userId));
	}
	
	/**
	 * 获取所有讲师信息
	 */
	@RequestMapping(value="/getTeacherList",method=RequestMethod.GET)
	@ApiOperation(value="获取所有讲师信息")
	public Map<String,Object> getTeacherList(@ModelAttribute @Valid BaseCo co){
		return courseService.getTeacherCourseList(co);
	}
}
